package com.test.mywebtest.controller;


import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

@RestController
@RequestMapping("/upload")
public class UploadController {


    @ResponseBody
    @PostMapping("/upload")
    public void upload(HttpServletRequest request){

        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;

        MultiValueMap<String, MultipartFile> multiFileMap = multipartHttpServletRequest.getMultiFileMap();

        System.out.println(multiFileMap.size());
        //try {
        //    ServletInputStream inputStream = request.getInputStream();
        //
        //    File file = new File("/Users/libin/Downloads/1.pdf");
        //
        //    try (OutputStream outputStream = new FileOutputStream(file)) {
        //        byte[] buffer = new byte[1024];
        //        int length;
        //
        //        while ((length = inputStream.read(buffer)) != -1) {
        //            outputStream.write(buffer, 0, length);
        //        }
        //    }
        //} catch (Exception e) {
        //    e.printStackTrace();
        //}

        System.out.println("---");
    }
}
